//北斗定位controller
//李志军

interface BDScope extends ng.IScope{
    location_button:string;
    carid;
    CarNo;
    isdisabled2:boolean;
    creationTime;
    area:string;
    phoneNumber;
    areaS:string;
    creationTimeS;
    shipAddress;
    deliverAddress;
    getBDNPLocation:()=>void;
    statusCode:string;
    goback:()=>void;
    format:any;
}
interface BDParams extends angular.ui.IStateParamsService{
    id;
    carcode;
    phone;
    shipAddress;
    deliverAddress;
}

class BDNPLocation{
    constructor($scope:BDScope,locationService:locationService,$stateParams:BDParams,$window:ng.IWindowService, private $state: angular.ui.IStateService){
        $scope.location_button = "定位";
        
        this.carid = $scope.carid =$stateParams.id;
        
                
        this.CarNo = $scope.CarNo = $stateParams.carcode;
        

        $scope.phoneNumber = $stateParams.phone;
        

        this.shipAddress = $scope.shipAddress = $stateParams.shipAddress
        


        this.deliverAddress = $scope.deliverAddress = $stateParams.deliverAddress;
        

        this.isdisabled = $scope.isdisabled2 = false;
        this.creationTime = $scope.creationTime = "";
        this.area = $scope.area = "";
        
        $scope.areaS = "";
        $scope.creationTimeS = "";
        //起始点名称数组       

        $scope.getBDNPLocation =  ()=> {
            $scope.location_button = "定位中，请稍等...";
            $scope.isdisabled2 = true;
            $scope.creationTime = "";
            $scope.statusCode = "";
            $scope.area = "";
            this.initMap();
            for (var i = 0; i < this.adds.length; ++i) {
                this.s_e(this.adds[i]);
            }
            
            locationService.getBDNPLocation(this.CarNo).then(
                 (resq)=> {
                    //console.log(resq);
                    $scope.area = resq.data.adr;
                    if (resq.data.utc != null) {
                        $scope.creationTime = resq.data.utc;
                    } else {
                        $scope.creationTime = new Date().getTime();
                    }
                    
                    $scope.statusCode = "定位成功";
                    $scope.isdisabled2 = false;
                    // 百度API
                    var p0 = parseFloat(resq.data.lon) / 600000;//经度
                    var p1 = parseFloat(resq.data.lat) / 600000;//纬度
                    var pt = new BMap.Point(p0, p1);
                    this.addMarker2(pt, new BMap.Label($scope.area, { offset: new BMap.Size(39, -19) }));
                    
                    $scope.location_button = "定位";
                   
                },

                 (resl) =>{
                    //console.log(resl);
                    $scope.statusCode = "定位失败";
                    $scope.location_button = "定位";
                    
                    $scope.isdisabled2 = true;
                }
            );
           
        }

        //返回按钮
       $scope.goback=() =>{
           this.$state.go('app.order.orderDeliverManage',{ name:'Manage'});
        }
        this.initMap();

    }
    carid;

    //定位API接口必要参数
    
    //页面各项初始化
    CarNo;
    //var carCode = $scope.carCode = '苏NM5052';
    isdisabled:boolean;
    creationTime:string;
    statusCode:string;
    area:string;
   
    //起始点名称数组
    shipAddress;
    deliverAddress;
    adds = [this.shipAddress, this.deliverAddress];
    map:any;

            //创建和初始化地图函数：
    initMap=() =>{
            this.createMap(); //创建地图
            this.setMapEvent(); //设置地图事件
            this.addMapControl(); //向地图添加控件
        }
        //创建地图函数：
    createMap=()=> {
        this.map = new BMap.Map("dituContent"); //在百度地图容器中创建一个地图
        let point = new BMap.Point(119.446448, 32.164418); //定义一个中心点坐标
        this.map.centerAndZoom(point, 7); //设定地图的中心点和坐标并将地图显示在地图容器中
        (<any>window).map = this.map; //将map变量存储在全局
    }

    //地图事件设置函数：
    setMapEvent=()=>{
        this.map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
    }

    //地图控件添加函数：
    addMapControl=()=> {
        //向地图中添加缩放控件
        var ctrl_nav = new BMap.NavigationControl({
            anchor: BMAP_ANCHOR_TOP_LEFT,
            type: BMAP_NAVIGATION_CONTROL_LARGE
        });
        this.map.addControl(ctrl_nav);
        //向地图中添加比例尺控件
        var ctrl_sca = new BMap.ScaleControl({
            anchor: BMAP_ANCHOR_BOTTOM_LEFT
        });
        this.map.addControl(ctrl_sca);
    }

    
    // 编写创建标注函数
    addMarker=(point, label)=> {
        var marker = new BMap.Marker(point);
        this.map.addOverlay(marker);
        marker.setLabel(label);

    }
    
    // 编写创建标注函数-当前定位点
    addMarker2=(point, label)=> {
        
        var myIcon = new BMap.Icon("/src/img/hc.png", new BMap.Size(48, 28));
        var marker2 = new BMap.Marker(point, { icon: myIcon });  // 创建标注
        this.map.addOverlay(marker2);              // 将标注添加到地图中
        marker2.setLabel(label);
        //marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
        this.map.centerAndZoom(point, 11);
    }

    ////逆地址解析函数,起始点使用
    s_e=(adds)=> {
        this.myGeo.getPoint(adds,  (point)=> {
            if (point) {
                var address = new BMap.Point(point.lng, point.lat);
                this.addMarker(address, new BMap.Label(adds, {
                    offset: new BMap.Size(20, -10)
                }));
            }
        });
    }

     
   
     //unix时间戳转化为常用时间
        // normaltime=(timer)=> {
        //     var newDate = new Date();
        //     newDate.setTime(timer - 1000);
            
        //     return String(this.defaultTimeEvent(newDate, "yyyy/MM/dd HH:mm:ss"));
        // }
        
        defaultTimeEvent(time: Date, type: any): void {
        var t = time;
        var tf = function (i) { return (i < 10 ? '0' : '') + i };
        return type.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
            switch (a) {
                case 'yyyy':
                    return tf(t.getFullYear());
                case 'MM':
                    return tf(t.getMonth() + 1);
                case 'mm':
                    return tf(t.getMinutes());
                case 'dd':
                    return tf(t.getDate());
                case 'HH':
                    return tf(t.getHours());
                case 'ss':
                    return tf(t.getSeconds()+1);
            }
        });
    }
       
        //获取当前时间
        CurentTime=()=> {
            var now = new Date();

            var year = now.getFullYear();       //年
            var month = now.getMonth() + 1;     //月
            var day = now.getDate();            //日

            var hh = now.getHours();            //时
            var mm = now.getMinutes();          //分
            var ss = now.getSeconds();          //秒
            var clock = year + "/";

            if (month < 10)
                clock += "0";

            clock += month + "/";

            if (day < 10)
                clock += "0";

            clock += day + " ";

            if (hh < 10)
                clock += "0";

            clock += hh + ":";
            if (mm < 10) clock += '0';
            clock += mm + ":";

            if (ss < 10) clock += '0';
            clock += ss;
            
            return (clock);
        }

    myGeo = new BMap.Geocoder();


}

angular.module("tms").controller("BDNPLocation", ["$scope",  "locationService", "$stateParams","$window","$state",BDNPLocation]);